package com.linkhub.oms.admin.modules.whitelist.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaMode;
import com.linkhub.oms.admin.modules.whitelist.domain.param.WhiteListInsertParam;
import com.linkhub.oms.admin.modules.whitelist.domain.vo.WhiteListApprovalVO;
import com.linkhub.oms.admin.modules.whitelist.service.WhiteListApprovalBizService;
import com.linkhub.oms.common.constant.RoleConstant;
import com.linkhub.oms.common.response.DataResponse;
import com.linkhub.oms.common.response.PageDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * @Description: 白名单服务入口类
 * @Author: echo
 * @Date: 2024/3/5 21:43
 * @Version: 1.0
 */
@Slf4j
@RestController
@Api(tags = {"白名单模块接口"})
@RequestMapping("/white_list")
@SaCheckLogin
public class WhiteListController {

    @Resource
    private WhiteListApprovalBizService whiteListApprovalBizService;

    @GetMapping("/approval/record")
    @ApiOperation(value = "查询白名单审批记录接口")
    @SaCheckRole(value = {RoleConstant.SUPER_ADMIN, RoleConstant.EMPLOYEE}, mode = SaMode.OR)
    public DataResponse<PageDTO<WhiteListApprovalVO>> listApprovalRecords(@ApiParam(value = "处理状态：0-待处理; null-所有") @RequestParam(value = "processStatus", required = false) Integer processStatus,
                                                                          @ApiParam(value = "邮箱搜索关键词") @RequestParam(value = "keyword", defaultValue = "") String keyword,
                                                                          @ApiParam(value = "页数, 从1开始") @RequestParam(value = "page", defaultValue = "1") Integer page,
                                                                          @ApiParam(value = "每页大小") @RequestParam(value = "size", defaultValue = "20") Integer size) {

        return DataResponse.success(whiteListApprovalBizService.pageWhiteListApprovals(keyword, processStatus, page, size));
    }

    @PutMapping("/approval/{id}")
    @ApiOperation(value = "单个审批接口")
    @SaCheckRole(value = {RoleConstant.SUPER_ADMIN, RoleConstant.EMPLOYEE}, mode = SaMode.OR)
    public DataResponse<Void> approveById(@PathVariable("id") Long id,
                                          @ApiParam(value = "审批状态：1-同意；2-拒绝")
                                          @RequestParam("approvalStatus") @Range(min = 1, max = 2, message = "{validation.approvalStatus.range}") Integer approvalStatus) {
        whiteListApprovalBizService.approveById(id, approvalStatus);
        return DataResponse.success();
    }


    @PostMapping("/batch")
    @ApiOperation(value = "批量添加白名单接口")
    @SaCheckRole(value = {RoleConstant.SUPER_ADMIN, RoleConstant.EMPLOYEE}, mode = SaMode.OR)
    public DataResponse<Void> addBatch(@Valid @RequestBody WhiteListInsertParam whiteListInsertParam) {
        whiteListApprovalBizService.addBatch(whiteListInsertParam);
        return DataResponse.success();
    }
}
